<template>
	<view class="home-sidebar-body">
		<u-list height="100%" :scrollIntoView="'item' + (selectTitleIndex)">
			<view v-for="(item, index) in titleList" :key="index" class="home-chat-item" :id="'item' + index"
			      :class="index === curSelectIndex ? 'home-chat-item-selected' : ''" @click="selectTitle(index)">
				<view class="home-chat-item-title">{{ item.topic ? item.topic : defaultTopic }}</view>
				<view class="home-chat-item-info">
					<view class="home-chat-item-count">{{ item.messages ? item.messages.length : 0 }}条对话</view>
					<view class="home-chat-item-date">{{ item.lastUpdate ? item.lastUpdate : "" }}</view>
				</view>
				<view class="home-chat-item-delete" @click.stop="deleteTitle(index)">
					<u-icon name="close-circle" color=""></u-icon>
				</view>
			</view>
		</u-list>
	</view>
</template>

<script>
export default {
	emits: ["selectTitle", "deleteTitle"],
	props: {
		titleList: {
			type: Array
		},
		curSelectIndex: {
			type: Number
		}
	},
	data() {
		return {
			selectTitleIndex: "",
			defaultTopic: uni.$u.chat.defaultTopic,
		}
	},
	created() {
		this.selectTitleIndex = this.curSelectIndex;
	},
	methods: {
		selectTitle(index) {
			this.$emit("selectTitle", index);
		},
		deleteTitle(index) {
			this.titleList.splice(index, 1);
			if (this.titleList.length === 0) {
				this.titleList.push(uni.$u.chat.newChatDefault());
			}
			if (index <= this.curSelectIndex) {
				this.selectTitle(this.curSelectIndex - 1 > 0 ? this.curSelectIndex - 1 : 0);
			}
			this.$emit("deleteTitle", index);
		}
	}
}
</script>

<style lang="scss">

@import "../css/chat.scss";

.u-icon {
	color: $primary;
}

.home-sidebar-body {
	flex: 1 1;
	overflow: auto;
}

.home-sidebar-body .home-chat-item-selected {
	border-color: $primary;
}

.home-chat-item {
	padding: 10px 14px;
	background-color: $white;
	border-radius: 10px;
	margin-bottom: 10px;
	box-shadow: $card-shadow;
	transition: background-color .3s ease;
	cursor: pointer;
	-webkit-user-select: none;
	-moz-user-select: none;
	user-select: none;
	border: 2px solid transparent;
	position: relative;
	overflow: hidden;
}

.home-chat-item-title {
	font-size: 14px;
	font-weight: 700;
	display: block;
	width: 200px;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.home-chat-item-info {
	display: flex;
	justify-content: space-between;
	align-items: center;
	color: #a6a6a6;
	font-size: 12px;
	margin-top: 8px;
}

.home-chat-item-count,
.home-chat-item-date {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.home-chat-item-delete {
	position: absolute;
	top: 10px;
	right: -20px;
	transition: all 0.3s ease;
	opacity: 0;
	cursor: pointer;
}

.home-chat-item:hover {
	background-color: $hover-color;
}

.home-chat-item:hover > .home-chat-item-delete {
	opacity: .5;
	right: 10px;
}

.home-chat-item:hover > .home-chat-item-delete:hover {
	opacity: 1;
}

</style>